/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */

package com.googlecode.bricket.plugin.quickstart.web.helloworld;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.ui.themes.ThemeUiHelper.IconEnum;

import com.googlecode.bricket.plugin.quickstart.domain.HelloWorld;
import com.googlecode.bricket.repository.GenericDomainService;
import com.googlecode.bricket.web.common.ActionPanelBuilder;
import com.googlecode.bricket.web.common.BricketAjaxLink;
import com.googlecode.bricket.web.common.BricketPanel;
import com.googlecode.bricket.web.common.DeleteActionLink;
import com.googlecode.bricket.web.common.EditActionLink;
import com.googlecode.bricket.web.common.table.AjaxFallbackDefaultDataTableBuilder;
import com.googlecode.bricket.web.common.table.DefaultDataProvider;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@AuthorizeInstantiation("ROLE_USER")
public class ManageHelloWorldPanel extends BricketPanel {
	@SpringBean
	private GenericDomainService genericDomainService;

	public ManageHelloWorldPanel(String id) {
		super(id);
		add(getHelloWorldTable("table"));
		
		setHeadline(new StringResourceModel("helloworld.headline", ManageHelloWorldPanel.this, null));

		addButton(new BricketAjaxLink(new StringResourceModel("create.label", ManageHelloWorldPanel.this, null), IconEnum.PLUS) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				// feedback
				getSession().cleanupFeedbackMessages();
				target.addComponent(getFeedback());
				// create
				EditOrCreateHelloWorldPanel edit = new EditOrCreateHelloWorldPanel(ManageHelloWorldPanel.this.getId());
				ManageHelloWorldPanel.this.getParent().addOrReplace(edit.setOutputMarkupId(true));
				target.addComponent(edit);
			}
		});
	}

	@SuppressWarnings("unchecked")
	private Component getHelloWorldTable(String id) {
		return AjaxFallbackDefaultDataTableBuilder.getBuilder(ManageHelloWorldPanel.this)
		.addDataProvider(new DefaultDataProvider<HelloWorld>(new HelloWorld()))
		.add(new AbstractColumn(new StringResourceModel("helloworld.actions.label", ManageHelloWorldPanel.this, null)) {
			@Override
			public void populateItem(Item cellItem, String componentId, IModel rowModel) {
				final ActionPanelBuilder linkBuilder = ActionPanelBuilder.getBuilder();
				final HelloWorld entry = (HelloWorld) rowModel.getObject();
				// edit link
				linkBuilder.add(new EditActionLink(true) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						// feedback
						getSession().cleanupFeedbackMessages();
						target.addComponent(getFeedback());
						// edit
						EditOrCreateHelloWorldPanel edit = new EditOrCreateHelloWorldPanel(ManageHelloWorldPanel.this.getId(), entry);
						ManageHelloWorldPanel.this.getParent().addOrReplace(edit.setOutputMarkupId(true));
						target.addComponent(edit);
					}
				});
				// delete link
				DeleteActionLink removeLink = new DeleteActionLink(new StringResourceModel("helloworld.actions.delete.confirm", ManageHelloWorldPanel.this, null), true) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						// feedback
						getSession().cleanupFeedbackMessages();
						target.addComponent(getFeedback());
						// delete
						genericDomainService.delete(HelloWorld.class, entry);
						// manage
						ManageHelloWorldPanel manage = new ManageHelloWorldPanel(ManageHelloWorldPanel.this.getId());
						ManageHelloWorldPanel.this.getParent().addOrReplace(manage.setOutputMarkupId(true));
						target.addComponent(manage);
					}
				};
				linkBuilder.add(removeLink);
				cellItem.add(linkBuilder.build(componentId));
			}
		})				
		.addPropertyColumn("id", true)
		.addPropertyColumn("description", true)
		.setNumberOfRows(10)
		.build(id);
	}
}
